﻿using System;
using System.Data;
using System.Text;
using MySql.Data.MySqlClient;
using Maticsoft.DBUtility;//Please add references
namespace hcrainMvc.DAL
{
	/// <summary>
	/// 数据访问类:sysbutton
	/// </summary>
	public partial class sysbutton
	{
		public sysbutton()
		{}
		#region  BasicMethod

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(long BtnID)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from sysbutton");
			strSql.Append(" where BtnID=@BtnID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BtnID", MySqlDbType.Int64)
			};
			parameters[0].Value = BtnID;

			return DbHelperMySQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public bool Add(hcrainMvc.Model.sysbutton model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into sysbutton(");
			strSql.Append("BtnIDHtml,BtnName,BtnText,BtnClass,BtnIcon,BtnScript,TabMenuId,BtnNo)");
			strSql.Append(" values (");
			strSql.Append("@BtnIDHtml,@BtnName,@BtnText,@BtnClass,@BtnIcon,@BtnScript,@TabMenuId,@BtnNo)");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BtnIDHtml", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnName", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnText", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnClass", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnIcon", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnScript", MySqlDbType.VarChar,50),
					new MySqlParameter("@TabMenuId", MySqlDbType.Double),
					new MySqlParameter("@BtnNo", MySqlDbType.Int32,11)};
			parameters[0].Value = model.BtnIDHtml;
			parameters[1].Value = model.BtnName;
			parameters[2].Value = model.BtnText;
			parameters[3].Value = model.BtnClass;
			parameters[4].Value = model.BtnIcon;
			parameters[5].Value = model.BtnScript;
			parameters[6].Value = model.TabMenuId;
			parameters[7].Value = model.BtnNo;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(hcrainMvc.Model.sysbutton model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update sysbutton set ");
			strSql.Append("BtnIDHtml=@BtnIDHtml,");
			strSql.Append("BtnName=@BtnName,");
			strSql.Append("BtnText=@BtnText,");
			strSql.Append("BtnClass=@BtnClass,");
			strSql.Append("BtnIcon=@BtnIcon,");
			strSql.Append("BtnScript=@BtnScript,");
			strSql.Append("TabMenuId=@TabMenuId,");
			strSql.Append("BtnNo=@BtnNo");
			strSql.Append(" where BtnID=@BtnID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BtnIDHtml", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnName", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnText", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnClass", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnIcon", MySqlDbType.VarChar,50),
					new MySqlParameter("@BtnScript", MySqlDbType.VarChar,50),
					new MySqlParameter("@TabMenuId", MySqlDbType.Double),
					new MySqlParameter("@BtnNo", MySqlDbType.Int32,11),
					new MySqlParameter("@BtnID", MySqlDbType.Int64,20)};
			parameters[0].Value = model.BtnIDHtml;
			parameters[1].Value = model.BtnName;
			parameters[2].Value = model.BtnText;
			parameters[3].Value = model.BtnClass;
			parameters[4].Value = model.BtnIcon;
			parameters[5].Value = model.BtnScript;
			parameters[6].Value = model.TabMenuId;
			parameters[7].Value = model.BtnNo;
			parameters[8].Value = model.BtnID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(long BtnID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sysbutton ");
			strSql.Append(" where BtnID=@BtnID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BtnID", MySqlDbType.Int64)
			};
			parameters[0].Value = BtnID;

			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string BtnIDlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from sysbutton ");
			strSql.Append(" where BtnID in ("+BtnIDlist + ")  ");
			int rows=DbHelperMySQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.sysbutton GetModel(long BtnID)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select BtnID,BtnIDHtml,BtnName,BtnText,BtnClass,BtnIcon,BtnScript,TabMenuId,BtnNo from sysbutton ");
			strSql.Append(" where BtnID=@BtnID");
			MySqlParameter[] parameters = {
					new MySqlParameter("@BtnID", MySqlDbType.Int64)
			};
			parameters[0].Value = BtnID;

			hcrainMvc.Model.sysbutton model=new hcrainMvc.Model.sysbutton();
			DataSet ds=DbHelperMySQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public hcrainMvc.Model.sysbutton DataRowToModel(DataRow row)
		{
			hcrainMvc.Model.sysbutton model=new hcrainMvc.Model.sysbutton();
			if (row != null)
			{
				if(row["BtnID"]!=null && row["BtnID"].ToString()!="")
				{
					model.BtnID=long.Parse(row["BtnID"].ToString());
				}
				if(row["BtnIDHtml"]!=null)
				{
					model.BtnIDHtml=row["BtnIDHtml"].ToString();
				}
				if(row["BtnName"]!=null)
				{
					model.BtnName=row["BtnName"].ToString();
				}
				if(row["BtnText"]!=null)
				{
					model.BtnText=row["BtnText"].ToString();
				}
				if(row["BtnClass"]!=null)
				{
					model.BtnClass=row["BtnClass"].ToString();
				}
				if(row["BtnIcon"]!=null)
				{
					model.BtnIcon=row["BtnIcon"].ToString();
				}
				if(row["BtnScript"]!=null)
				{
					model.BtnScript=row["BtnScript"].ToString();
				}
					//model.TabMenuId=row["TabMenuId"].ToString();
				if(row["BtnNo"]!=null && row["BtnNo"].ToString()!="")
				{
					model.BtnNo=int.Parse(row["BtnNo"].ToString());
				}
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select BtnID,BtnIDHtml,BtnName,BtnText,BtnClass,BtnIcon,BtnScript,TabMenuId,BtnNo ");
			strSql.Append(" FROM sysbutton ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM sysbutton ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.BtnID desc");
			}
			strSql.Append(")AS Row, T.*  from sysbutton T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperMySQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			MySqlParameter[] parameters = {
					new MySqlParameter("@tblName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@fldName", MySqlDbType.VarChar, 255),
					new MySqlParameter("@PageSize", MySqlDbType.Int32),
					new MySqlParameter("@PageIndex", MySqlDbType.Int32),
					new MySqlParameter("@IsReCount", MySqlDbType.Bit),
					new MySqlParameter("@OrderType", MySqlDbType.Bit),
					new MySqlParameter("@strWhere", MySqlDbType.VarChar,1000),
					};
			parameters[0].Value = "sysbutton";
			parameters[1].Value = "BtnID";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperMySQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

